import asyncio

from com.arcfox.base.base_spider import ExecuteType
from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.school.processor.yzy.yzy_province_score_processor import YZYProvinceScoreProcessor
from com.arcfox.school.processor.yzy.yzy_university_score_processor import YZYUniversityScoreProcessor
from com.arcfox.school.spider.yzy.yzy_base_spider import YZYBaseSpider
from com.arcfox.util import async_request as requests
from com.arcfox.util.redis_key_manager import YZY_UNIVERSITY_SCHOOL_LIST_KEY, YZY_UNIVERSITY_PROVINCE_SCHOOL_LIST_KEY
from loguru import logger


class YZYUniversityScoreSpider(YZYBaseSpider):
    '''
    优志愿学校列表抓取, 用列表数据抓取学校相关的图片, 以及学校编码更新到tb_university_info
    '''

    def __init__(self):
        super().__init__()
        self.url = "https://uwf7de983aad7a717eb.youzy.cn/youzy.dms.datalib.api.enrolldata.enter.college.encrypted.v2.get"
        self.start_page = 1
        self.processor = YZYUniversityScoreProcessor()
        self.task_manager = RedisTaskManager(YZY_UNIVERSITY_PROVINCE_SCHOOL_LIST_KEY)

    def get_params(self, unversity_id, p_code, ):
        return '{"collegeCode":' + str(unversity_id) + ',"provinceCode":' + str(p_code) + '}'

    def get_headers(self, params):
        return {
            "Host": "uwf7de983aad7a717eb.youzy.cn",
            "u-sign": self.get_u_sgin(params),
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
            "Referer": "https://pv4y-pc.youzy.cn/",
        }

    async def _pull_task(self):
        return await self.task_manager.pull_tasks(10), ExecuteType.FINISH

    async def _crawl_by_task(self, tasks):
        for task in tasks:
            p_map = self.get_province_map_reverse()
            for p_code in p_map:
                try:
                    params = self.get_params(task["yzy_code"], p_code)
                    # params = self.get_params("10029", 11)
                    # logger.info("p_code:{}", p_code)
                    resp = await requests.post_with_session(url=self.url, data=params, headers=self.get_headers(params))
                    if resp.code == 200:
                        result_json = resp.json()
                        await self.processor.parse_and_save_data(result_json,p_map,task["university_name"])
                except Exception as e:
                    logger.error("{}爬取失败,{}", task, e)
                    await self.task_manager.add_fail_tasks(task)


if __name__ == "__main__":
    asyncio.run(YZYUniversityScoreSpider().start_crawl())
